package com.xiaolin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaolin.admin.exception.ServiceException;
import com.xiaolin.admin.mapper.SysPostMapper;
import com.xiaolin.admin.mapper.SysUserMapper;
import com.xiaolin.admin.model.dto.CurrentUser;
import com.xiaolin.admin.model.entity.SysPost;
import com.xiaolin.admin.model.entity.SysUser;
import com.xiaolin.admin.service.SysPostService;
import com.xiaolin.admin.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author xiaolin
 * @since 2024-07-22
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    @Autowired
    private SysPostMapper postMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ContextUtil contextUtil;

    @Override
    public List<SysPost> getPostList(SysPost post) {
        return postMapper.getPostList(post);
    }

    @Override
    public PageInfo<SysPost> getPostPage(Integer page, Integer pageSize, SysPost post) {
        PageHelper.startPage(page, pageSize);
        List<SysPost> postList = postMapper.getPostList(post);
        return new PageInfo<>(postList);
    }

    /**
     * 校验岗位唯一性
     *
     * @param post
     * @return
     */
    @Override
    public void checkUnique(SysPost post) {
        long count = postMapper.selectCount(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostName, post.getPostName())
                .ne(Objects.nonNull(post.getId()), SysPost::getId, post.getId()));
        if (count > 0) {
            ServiceException.error("岗位名称重复了");
        }
    }

    @Override
    public void checkUserRelation(Integer postId) {
        long count = userMapper.selectCount(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPostId, postId));
        if (count > 0) {
            ServiceException.error("该岗位关联了用户，无法删除");
        }
    }

    @Override
    public boolean addPost(SysPost post) {
        CurrentUser user = contextUtil.getCurrentUser();
        // 校验岗位唯一性
        checkUnique(post);
        post.setCreator(user.getId());
        post.setUpdator(user.getId());
        postMapper.insert(post);
        return true;
    }

    @Override
    public SysPost getPost(Integer id) {
        return postMapper.selectById(id);
    }

    @Override
    public boolean updatePost(SysPost post) {
        // 校验岗位唯一性
        checkUnique(post);
        postMapper.updateById(post);
        return true;
    }

    @Override
    public boolean deletePost(Integer id) {
        // 校验岗位与用户是否关联
        checkUserRelation(id);
        postMapper.deleteById(id);
        return true;
    }
}
